<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>chainAsync</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const chainAsync = handlers => {
            let cur = 0;
            const last = handlers[handlers.length - 1];
            const next = () => {
                const fn = handlers[cur++];
                return fn === last ? fn() : fn(next);
            };
            next();
        };
        chainAsync([
            next => {
                console.log('0 seconds');
                setTimeout(next, 1000);
            },
            next => {
                console.log('1 second');
                setTimeout(next, 1000);
            },
            () => {
                console.log('2 second');
            }
        ]);
    </script>
</body>

</html>